<?
	error_reporting (E_ALL ^ E_NOTICE);
	session_start();
	
	include('../../initDB.php');
	include('../../querys.php');
	include('../../functions.php');
	
	$tags = NULL;
	$user = NULL;
	$type = NULL;
	$d    = NULL;
	$w    = NULL;
	$and = "0";
	$dateBegin = NULL;
	$dateEnd   = NULL;
	
	$parameters = array( 'tag' => $tags,
						 'user'=> $user,
						 'type'=> $type,
						 'and' => $and,
						 'd'   => $d,
						 'w'   => $w,
						 'dateBegin' => $dateBegin,
						 'dateEnd'   => $dateEnd
						);
						
	$querys = array( 'tag' => $queryTag,
					 'user'=> $queryUser,
					 'type'=> $queryType,
					 'd'   => $d,
					 'w'   => $w,
					 'dateBegin' => $dateBegin,
					 'dateEnd'   => $dateEnd
					);
					
	foreach($parameters as $key => &$value) {
		if( isset($_REQUEST[$key]) )
			if ( is_array($_REQUEST[$key])) {
				$value = array();
				foreach($_REQUEST[$key] as $manyTags) {
					$manyTags = escape_string($manyTags);
					if ($key != 'user')
						$manyTags = strtolower($manyTags);
					array_push($value, $manyTags);
					}
			}
			else {	
				$value = escape_string($_REQUEST[$key]);
				if ($value == '') {
					unset($parameters[$key]);
					}
				if ($key != 'user')
					$value = strtolower($value);
				}
		else if ($key != 'and')
			unset($parameters[$key]);
	}
	
	if (sizeof($parameters) == 0) {
		print_output('error 0: no valid paramaters');
		exit();
		}
		
	$resultSet = array();
	foreach($parameters as $variable => $values) {
		$sql = $querys[$variable][0];
		if (is_array($values)) {
			$it = 0;
			foreach($values as $vars) {
				$sql .= $querys[$variable][1] . add_quotes($vars); 
				$it += 1;
				if ($it != sizeof($values)) 
					$sql = $sql . $queryExclusive[$parameters['and']];
				}
				}
		else
			$sql .= $querys[$variable][1] . add_quotes($values);
		
		$resultSet[$variable] = $sql . $querys[$variable][2];
		$sql .= $querys[$variable][2];
	}
	
	# intersect
	if (isset($resultSet['tag']) && isset($resultSet['user'])) {
			$resultSet['final'] =  $resultSet['tag'] . $queryIntersect . $resultSet['user'] . ')';
			$resultSet['final'] = $queryIdToLink[0] . $resultSet['final'] . $queryIdToLink[2];
		}
	else if (isset($resultSet['tag']) || isset($resultSet['user'])) {
			$resultSet['final'] =  current($resultSet) . ')';
			$resultSet['final'] = $queryIdToLink[0] . $resultSet['final'] . $queryIdToLink[2];
		}
	else
		$resultSet['final'] = $queryBasic; 
	
	#if ($queryType != "") {
		$resultSet['final'] .= $orderQuery[$parameters['type']] ;
	#}	

	
	# execute sql
	#echo $resultSet['final'] . "\n\t\r";
	$db_getAll = $db->prepare($resultSet['final']);
	$db_getAll->execute();
	$result = $db_getAll->fetchAll();
	
	# add user & tag to each link
	foreach($result as &$row) {
		# user
		$id = $row['id'];
		$value = $row['linkValue'];
		$db_retrieve_user_link->execute(array($id));
		$users = $db_retrieve_user_link->fetchAll();
		$row['user'] = $users;
		
		# tag
		$db_retrieve_tags_by_id->execute(array($id));
		$linksTags = $db_retrieve_tags_by_id->fetchAll();
		$row['tags'] = $linksTags;
	}
	
	# clean the associative array
	clean_array($result, 4);
	print_output($result);
	
	# view-source:http://paginas.fe.up.pt/~ei09128/aulas/ltw/feup-mieic-ltw-1112-proj1/api/link/list.php?tag[]=world&tag[]=asia&user=Smith	
?>